package com.gitee.qdbp.general.common.web.controller.reusable;

import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;
import javax.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import com.alibaba.fastjson.JSON;
import com.gitee.qdbp.able.exception.ServiceException;
import com.gitee.qdbp.able.result.ResponseMessage;
import com.gitee.qdbp.able.result.ResultCode;
import com.gitee.qdbp.base.annotation.OperateRecord;
import com.gitee.qdbp.tools.http.HttpException;
import com.gitee.qdbp.tools.http.HttpTools;
import com.gitee.qdbp.tools.http.HttpUrl;
import com.gitee.qdbp.tools.http.HttpUrl.HostUrl;
import com.gitee.qdbp.tools.http.RemoteServiceException;
import com.gitee.qdbp.tools.utils.PropertyTools;

/**
 * 页面转发控制器<br>
 * 示例: forward/manager/actions/xxx/yyy.json<br>
 * 配置: site.manager.inner.url = http://127.0.0.1:8888/m/<br>
 * 结果: 返回http://127.0.0.1:8888/m/actions/xxx/yyy.json的数据
 * 
 * @author zhaohuihua
 * @version 170704
 */
@Controller
@RequestMapping("forward")
@OperateRecord(enable = false)
public class ForwardController {

    @Autowired
    @Qualifier("setting")
    private Properties setting;

    @ResponseBody
    @RequestMapping(value = "/{project}/**", method = RequestMethod.POST)
    public ResponseMessage forward(@PathVariable("project") String project, HttpServletRequest request)
            throws ServiceException {

        String key = "site." + project + ".inner.url";
        String prefix = PropertyTools.getString(setting, key, true);
        if (prefix == null) {
            throw new ServiceException(ResultCode.PARAMETER_VALUE_ERROR);
        }

        HostUrl host = new HostUrl(prefix);

        String uri = request.getRequestURI();
        String find = "/forward/" + project + "/";
        int index = uri.indexOf(find);
        uri = uri.substring(index + find.length());

        HttpUrl url = "GET".equalsIgnoreCase(request.getMethod()) ? host.get(uri) : host.post(uri);

        Map<String, Object> params = new HashMap<>();
        Enumeration<String> names = request.getParameterNames();
        while (names.hasMoreElements()) {
            String name = names.nextElement();
            params.put(name, request.getParameter(name));
        }

        try {
            ResponseMessage resp = HttpTools.form.execute(url, params);
            Object object = resp.getBody();
            if (object instanceof String) {
                resp.setBody(JSON.parse((String) object));
            }
            return resp;
        } catch (RemoteServiceException e) {
            throw new ServiceException(e);
        } catch (HttpException e) {
            throw new ServiceException(ResultCode.REMOTE_SERVICE_ERROR, e);
        }
    }

}
